using Microsoft.AspNetCore.Mvc;

using StateManagement.Models;

namespace StateManagement.Controllers {
  public class SessionController : Controller {
    public IActionResult Index() {
      var session = HttpContext.Session;
      int counter = 1 + (session.GetInt32("Counter") ?? 0);
      session.SetInt32("Counter", counter);
      return View(counter);
    }

    public string AddBook() {
      HttpContext.Session.SetObject("book", new Book() { 
        Id=1,
        ISBN = "123-4-567-8-9012",
        Title = "How to be rich in a week?",
        Author = "C.K.Leng",
        Year = 2025,
        Price = 999.99f,
      });
      return "Book added";
    }

    public IActionResult GetBook() {
      var book = HttpContext.Session.GetObject<Book>("book");
      if (book == null) return NotFound();
      else return View("GetBook2",book);
    }
    static Book book1 = new Book() { Id = 100, ISBN = "888", Author = "Alibaba", Year = 2000, Price = 88.8f, Title = "ASP.NET Core is Fun!" };
    static Book book2 = new Book() { Id = 101, ISBN = "999", Author = "Abumama", Year = 2001, Price = 99.9f, Title = "Web Development with ASP.NET Core" };
    public void AddCart() {
      var cart = HttpContext.Session.GetObject<Cart<Book>>("cart");
      if (cart==null) cart = new Cart<Book>();
      cart.Add(book1);
      cart.Add(book2, 3);
      HttpContext.Session.SetObject("cart", cart);
      //return View("ShowCart", cart);
      //return RedirectToAction("ShowCart");
    }

    public IActionResult ShowCart() {
      var cart = HttpContext.Session.GetObject<Cart<Book>>("cart");
      if (cart == null) return NotFound();
      else return View(cart);
    }


  }
}
